package com.xmb.wechat.adapter;

import android.content.Context;
import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatContactBean_;
import com.xmb.wechat.bean.WechatLastMsgBean;
import com.xmb.wechat.util.SpanStringUtils;
import com.xmb.wechat.util.WechatDateFormat;

import java.util.List;

import io.objectbox.Box;

/**
 * @Author lijiangop
 * @CreateTime 2020/6/8 11:50
 */
public class WechatLastMsgAdapter extends BaseQuickAdapter<WechatLastMsgBean, BaseViewHolder> {

    private Box<WechatContactBean> mWechatContactBeanBox;

    public WechatLastMsgAdapter(Context context, int layoutResId, @Nullable List<WechatLastMsgBean> data) {
        super(layoutResId, data);
        mContext = context;
        mWechatContactBeanBox = WechatStartApplication.getBoxStore().boxFor(WechatContactBean.class);
    }

    @Override
    protected void convert(BaseViewHolder helper, WechatLastMsgBean vo) {
        if (vo == null)
            return;
        helper.setText(R.id.content, SpanStringUtils.getOnlyEmotion(mContext, helper.getView(R.id.content), vo.getMsgContent()));
        //未读数量：
        helper.setText(R.id.tv_msg_count, vo.getUnReadCount() + "");
        helper.setVisible(R.id.tv_msg_count,
                vo.getUnReadCount() > 0 && vo.getUnReadCount() < 100);
        helper.setVisible(R.id.iv_msg_count,
                vo.getUnReadCount() >= 100);
        helper.setText(R.id.time, WechatDateFormat.formatOutsideMsgList(vo.getTime()));

        //头像,名字
        WechatContactBean contactBean = mWechatContactBeanBox.query().equal(WechatContactBean_.id, vo.getTalkerID()).build().findUnique();
        if (contactBean == null)
            return;
        helper.setText(R.id.name, contactBean.getName());
        WechatContactBean.setupAvatarIntoImageView(contactBean.getAvatarSrc(), null, contactBean.getAvatar(), helper.getView(R.id.iv_avatar), 12);
    }

}
